package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sc.nft.dto.TeamHoldCollectionVO;
import com.sc.nft.dto.UserHoldCollectionDTO;
import com.sc.nft.dto.UserHoldVO;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.UserCollection;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.BoxGroupListDTO;
import com.sc.nft.entity.dto.CollectionLogDTO;
import com.sc.nft.entity.dto.MallUserCollectionDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.metaWalletDTO.SimpleUserCollectionVO;
import com.sc.nft.enums.*;
import com.sc.nft.mapper.UserCollectionMapper;
import com.sc.nft.pool.UserPool;
import com.sc.nft.sup.SuperEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.sc.nft.enums.ChainStatusEnum.ONLINE;

/**
 * 用户藏片表(sc_user_collection)数据DAO
 *
 * @author zxy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-06-04 01:42:17
 */
@Slf4j
@Repository
public class UserCollectionDao extends ServiceImpl<UserCollectionMapper, UserCollection> {

    @Resource
    private UserInfoDao userInfoDao;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public Page<MyCollectionNowVO> myCollectionNow(String isPrivilege, Long userId, int pageNo, int pageSize) {
        return baseMapper.myCollectionNow(new Page(pageNo, pageSize), userId, isPrivilege);
    }

    public List<MyCollectionNowVO> myCollectionNowList(Long userId) {
        return baseMapper.myCollectionNowList(userId);
    }

    public MyCollectionNowVO countMyCollectionNow(Long userId, Long productId, Long taskPoolId) {
        return baseMapper.countMyCollectionNow(userId, productId, taskPoolId);
    }

    public Page<MyCollectionVO> myCollection(String isPrivilege, Long userId, int pageNo, int pageSize) {
        return baseMapper.myCollection(new Page(pageNo, pageSize), userId, isPrivilege);
    }

    public Page<MyBoxVO> myBox(Long userId, GiveStatusEnum giveStatus, int pageNo, int pageSize) {
        return baseMapper.myBox(new Page(pageNo, pageSize), userId, giveStatus.getCode());
    }

    public Page<MyBoxVO> myBoxByGiveStatus(Long userId, GiveStatusEnum giveStatus, int pageNo, int pageSize) {
        return baseMapper.myBoxByGiveStatus(new Page(pageNo, pageSize), userId, giveStatus.getCode());
    }

    public Page<CollectionLogDTO> giveCollectionLog(Long userId, int pageNo, int pageSize) {

        return baseMapper.giveCollectionLog(new Page(pageNo, pageSize), userId);
    }


    public Page<CollectionLogDTO> getCollectionLog(Long userId, int pageNo, int pageSize) {

        return baseMapper.getCollectionLog(new Page(pageNo, pageSize), userId);
    }

    public List<UserCollection> findOneByProductIdAndUserId(DigitalCollectionProductInfo digitalCollectionProductInfo, UserInfo userInfo) {
        LambdaQueryWrapper<UserCollection> userCollectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userCollectionLambdaQueryWrapper.eq(UserCollection::getCollectionId, digitalCollectionProductInfo.getRelationId());
        userCollectionLambdaQueryWrapper.eq(UserCollection::getUserId, userInfo.getId());
        userCollectionLambdaQueryWrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return list(userCollectionLambdaQueryWrapper);

    }

    public List<NotOpenBlindBoxVO> notOpenBlindBox(Long userId, Long boxId) {
        return baseMapper.notOpenBlindBox(userId, boxId);
    }

    public UserCollection getLastNotOpenBox(Long userId, Long boxId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getBoxId, boxId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.FALSE);
        wrapper.orderByAsc(UserCollection::getId);
        wrapper.last(" limit 1");
        return getOne(wrapper);
    }

    public List<UserCollection> getOneByGiveStatus() {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getGiveStatus, GiveStatusEnum.GIVING);
        wrapper.last("limit 200");
        return list(wrapper);
    }

    public boolean userHasCollection(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, true);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.last("limit 1");
        return ObjectUtil.isNotNull(getOne(wrapper));
    }

    public boolean userHasCollections(Long userId, List<Long> collectionIds) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, true);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.in(UserCollection::getCollectionId, collectionIds);
        wrapper.last("limit 1");
        return ObjectUtil.isNotNull(getOne(wrapper));
    }


    public Page<TransferVO> transferLog(String fromUserAddress, String toUserAddress, String transactionHash, String chainUrl, int pageNo, int pageSize) {
        return baseMapper.transferLog(new Page<>(pageNo, pageSize), fromUserAddress, toUserAddress, transactionHash, chainUrl);
    }

    public Page<UserCollectionVO> userCollection(String firstTitle, int pageNo, int pageSize, Date startTime, Date endTime, String userTel) {
        UserInfo oneByUserTel = null;
        if (StrUtil.isNotBlank(userTel)) {
            oneByUserTel = userInfoDao.getOneByUserTel(userTel);
        }

        return baseMapper.userCollection(new Page<>(pageNo, pageSize).setSearchCount(Boolean.FALSE).setTotal(baseMapper.userCollectionTole(firstTitle, startTime, endTime, BeanUtil.isNotEmpty(oneByUserTel) ? oneByUserTel.getId() : null)), firstTitle, startTime, endTime, BeanUtil.isNotEmpty(oneByUserTel) ? oneByUserTel.getId() : null);
    }

    public List<UserCollectionExcelVO> userCollectionExcel(String firstTitle, Date startTime, Date endTime, String userTel) {
        UserInfo oneByUserTel = null;
        if (StrUtil.isNotBlank(userTel)) {
            oneByUserTel = userInfoDao.getOneByUserTel(userTel);
        }

        List<UserCollectionExcelVO> userCollectionExcelVOS = baseMapper.userCollectionExcel(firstTitle, startTime, endTime, BeanUtil.isNotEmpty(oneByUserTel) ? oneByUserTel.getId() : null);
        return userCollectionExcelVOS;
    }

    public Integer getCountByCollectionId(Long consumeCollectionId, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, consumeCollectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        return count(wrapper);
    }

    public Integer getHoldCountByCollectionId(Long consumeCollectionId, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, consumeCollectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return count(wrapper);
    }

    public Integer getHoldOpenCountByCollectionId(Long consumeCollectionId, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, consumeCollectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        return count(wrapper);
    }

    public Integer getCountByCollectionNotConsignId(Long consumeCollectionId, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, consumeCollectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        return count(wrapper);
    }

    public List<Long> getHoldIdByCollectionNotConsignId(Long consumeCollectionId, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, consumeCollectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.lt(UserCollection::getLockedPositionTime, DateTime.now());
        return listObjs(wrapper, x -> (Long) x);
    }

    public Integer getNotHoardCountByCollectionId(Long consumeCollectionId, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, consumeCollectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getAntiHoardFlag, Boolean.FALSE);
        return count(wrapper);
    }


    public Boolean deleteCollectionByUserIdCollectionId(Long userId, Long collectionId, Integer num) {
        UpdateWrapper<UserCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("collection_id", collectionId);
        wrapper.eq("on_consign", CollectionOnConsignEnum.NORMAL.getCode());
        wrapper.eq("is_hold", Boolean.TRUE);
        wrapper.eq("is_open", Boolean.TRUE);
        wrapper.eq("is_smelting_only", Boolean.FALSE);
        wrapper.eq("status", ONLINE.getCode());
        wrapper.eq("freezing_status", UserCollectionFreezingStatusEnum.NORMAL.getCode());
        wrapper.set("is_hold", Boolean.FALSE);
        wrapper.orderByDesc("locked_position_time");
        wrapper.last("limit " + num);
        return update(wrapper);
    }


    public Boolean deleteCollectionByUserIdCollectionIdAndIds(Long userId, Long collectionId, Long id) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getId, id);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.set(UserCollection::getIsHold, Boolean.FALSE);
        return update(new UserCollection(), wrapper);
    }

    /**
     * 拍卖 质押转消耗
     *
     * @param id
     * @return
     */
    public Boolean deleteCollectionById(Long id) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.LOCK);
        wrapper.set(UserCollection::getIsHold, Boolean.FALSE);
        return update(new UserCollection(), wrapper);
    }

    public UserCollection getUserCollectionById(Long id) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        return getOne(wrapper);
    }

    public List<UserCollection> getAllPrivilegeByUserId(Long userId) {
        return baseMapper.getAllPrivilegeByUserId(userId);
    }


    public List<UserCollection> getListByCollercitonId(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.orderByAsc(UserCollection::getNumbering);
        return list(wrapper);
    }

    public UserCollection getMinNumCanUse(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.orderByAsc(UserCollection::getNumbering);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public Integer getCountByCollercitonIdAndUserId(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        return count(wrapper);
    }

    public List<UserCollection> getUserCollection(Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        return list(wrapper);
    }

    public UserCollection findByUuidOnHold(String collectionUuid, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getCollectionUuid, collectionUuid);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        return getOne(wrapper);
    }

    public Page<MyCollectionListByCollectionIdVO> myCollectionListByCollectionId(Long collectionId, Long userId, Integer pageNo, Integer pageSize) {
        return baseMapper.myCollectionListByCollectionId(collectionId, userId, new Page(pageNo, pageSize));
    }

    public Page<MyCollectionListByCollectionIdVO> maintainCollectionListByCollectionId(Long collectionId, Long userId,
                                                                                       Integer pageNo, Integer pageSize,
                                                                                       BigDecimal lowPrice, BigDecimal highPrice, CollectionOnConsignEnum onConsign) {
        return baseMapper.maintainCollectionListByCollectionId(collectionId, userId, lowPrice, highPrice, onConsign, new Page(pageNo, pageSize));
    }

    public Integer getCirculationNumByCollectionId(Long id) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getCollectionId, id);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        return count(wrapper);
    }


    public List<UserCollection> getNoNumbering() {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getNumbering, 0);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.lt(UserCollection::getCollectionId, 144L);
        return list(wrapper);
    }

    public List<UserCollection> getInitializationNumbering() {
        List<UserCollection> noNumbering = baseMapper.getNoNumbering();
        return noNumbering;
    }

    public void updateNumbering(Long id, int numbering) {
        baseMapper.updateNumbering(id, numbering);
    }

    public boolean updateChainUrl(Long id, String chainUrl, ChainStatusEnum status) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("chain_url='" + chainUrl + "',status =" + status.getCode() + ",modify_time=now()");
        wrapper.eq(UserCollection::getId, id);
        return update(wrapper);
    }

    public boolean setConsignStatus(Long id, CollectionOnConsignEnum consignStatus) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(UserCollection::getOnConsign, consignStatus);
        wrapper.eq(UserCollection::getId, id);
        return update(wrapper);
    }

    public boolean setConsignStatusAndEq(Long id, CollectionOnConsignEnum consignStatus, CollectionOnConsignEnum oldStatus) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(UserCollection::getOnConsign, consignStatus);
        wrapper.eq(UserCollection::getId, id);
        wrapper.eq(UserCollection::getOnConsign, oldStatus);
        return update(wrapper);
    }

    public List<ConsignCollectionListVO> getConsignCollectionList(Long userId, Long collectionId) {
        return baseMapper.getConsignCollectionList(userId, collectionId);
    }

    public List<ConsignCollectionListVO> getConsignCollectionListAdmin(Long userId, Long collectionId, Integer sum) {
        return baseMapper.getConsignCollectionListAdmin(userId, collectionId, sum);
    }


    public BigDecimal getIssuePrice(Long collectionId, Long userId) {
        BigDecimal price = baseMapper.getIssuePrice(collectionId, userId);
        return price == null ? BigDecimal.ZERO : price;
    }

    /**
     * 查询铸造中藏品
     *
     * @return
     */
    public List<UserCollection> getNotMintCollection() {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, true);
        wrapper.eq(UserCollection::getIsOpen, true);
        wrapper.eq(UserCollection::getStatus, ChainStatusEnum.NONE);
        wrapper.isNull(UserCollection::getGiveUserId);
        wrapper.last("limit 100");
        return list(wrapper);
    }

    /**
     * 查询交易未转移藏品
     *
     * @return
     */
    public List<UserCollection> getDealCollection() {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, true);
        wrapper.eq(UserCollection::getIsOpen, true);
        wrapper.eq(UserCollection::getStatus, ChainStatusEnum.NONE);
        wrapper.isNotNull(UserCollection::getGiveUserId);
        wrapper.last("limit 100");
        return list(wrapper);
    }

    /**
     * 查询待销毁藏品
     *
     * @return
     */
    public List<UserCollection> getBurnCollection() {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, false);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.isNull(UserCollection::getToUserId);
        wrapper.last("limit 100");
        return list(wrapper);
    }

    /**
     * 查询藏品赠送人
     *
     * @return
     */
    public UserCollection getCollectionGiveUser(String collectionUuid, Long giveUserId, Long toUserId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, false);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getCollectionUuid, collectionUuid);
        wrapper.eq(UserCollection::getUserId, giveUserId);
        wrapper.eq(UserCollection::getToUserId, toUserId);
        return getOne(wrapper);
    }

    public Boolean updateStatusAndTimeById(Long id, GiveStatusEnum status, DateTime time) {
        UpdateWrapper<UserCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("give_status", status.getCode());
        wrapper.set("give_success_time", time);
        return update(wrapper);
    }

    public Page<ConsignCollectionListByNewVO> getPageConsignCollectionListByQuery(Long userId, Long collectionId, Integer antiHoardFlag, Integer antiHoardPriceSort, BigDecimal charge, Integer pageNo, Integer pageSize) {
        Page<ConsignCollectionListByNewVO> page;
        if (antiHoardFlag == 1) {
            page = baseMapper.getPageConsignCollectionListByQuery(new Page(pageNo, pageSize), userId, collectionId, antiHoardFlag, antiHoardPriceSort, charge);
            List<ConsignCollectionListByNewVO> records = page.getRecords();
            List<ConsignCollectionListByNewVO> list = Lists.newArrayList();
            for (ConsignCollectionListByNewVO record : records) {
                BigDecimal toDayCollectionMaxPrice = record.getToDayCollectionMaxPrice();
                BigDecimal antiHoardSellPrice = record.getAntiHoardSellPrice();
                if (toDayCollectionMaxPrice.compareTo(antiHoardSellPrice) > -1) {
                    list.add(record);
                }
            }
            page.setRecords(list);
        } else {
            page = baseMapper.getPageConsignCollectionListByQuery(new Page(pageNo, pageSize), userId, collectionId, antiHoardFlag, antiHoardPriceSort, charge);
        }

        return page;
    }

    public List<UserCollection> getByCollectionId(Long id) {
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collection_id", id);
        queryWrapper.select("numbering");
        return baseMapper.selectList(queryWrapper);
    }

    public int getCountByCollectionIdAndUserId(Long id, Long userId) {
        LambdaQueryWrapper<UserCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCollection::getCollectionId, id);
        queryWrapper.eq(UserCollection::getUserId, userId);
        queryWrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        queryWrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        return count(queryWrapper);
    }

    public List<UserCollection> getByCollectionIdAndUserId(Long id, Long userId) {
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collection_id", id);
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_hold", Boolean.TRUE);
        queryWrapper.eq("is_open", Boolean.TRUE);
//        queryWrapper.eq("status", ChainStatusEnum.ONLINE);
        return baseMapper.selectList(queryWrapper);
    }

    public List<UserCollection> getByCollectionIdsAndUserId(List<Long> ids, Long userId) {
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("collection_id", ids);
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_hold", Boolean.TRUE);
        queryWrapper.eq("is_open", Boolean.TRUE);
//        queryWrapper.eq("status", ChainStatusEnum.ONLINE);
        queryWrapper.eq("on_consign", CollectionOnConsignEnum.NORMAL.getCode());
        queryWrapper.eq("give_status", GiveStatusEnum.NOT);
        return baseMapper.selectList(queryWrapper);
    }

    public List<UserCollection> getByProductIdsAndUserId(List<Long> ids, Long userId) {
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("collection_id", ids);
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_hold", Boolean.TRUE);
        queryWrapper.eq("is_open", Boolean.TRUE);
        queryWrapper.eq("status", ONLINE);
        queryWrapper.eq("give_status", GiveStatusEnum.NOT);
        queryWrapper.groupBy("collection_id");
        return baseMapper.selectList(queryWrapper);
    }

    public List<UserCollection> findListByCollectionId(Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return list(wrapper);
    }

    public UserCollection selectByIdAndUserId(Long id, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.eq(UserCollection::getUserId, userId);
        return getOne(wrapper);
    }

    public UserCollection selectByIdAndUserIdAndIsHold(Long id, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return getOne(wrapper);
    }


    /**
     * 获取用户城主权益卡
     *
     * @return
     */
    public UserCollection getUserApplyCreatedDaoCastellanRights(Long collectionId) {
        UserCollection userApplyCreatedDaoCastellanRights = baseMapper.getUserApplyCreatedDaoCastellanRights(UserPool.getUser().getId(), collectionId);
        return userApplyCreatedDaoCastellanRights;
    }

    public UserCollection getCollectionIdAndNumbering(Long collectionId, Integer numbering) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getNumbering, numbering);
        return getOne(wrapper);
    }


    public UserCollection getAdminUserApplyCreatedDaoCastellanRights(Long collectionId, Long userId) {
        UserCollection userApplyCreatedDaoCastellanRights = baseMapper.getUserApplyCreatedDaoCastellanRights(userId, collectionId);
        return userApplyCreatedDaoCastellanRights;
    }

    public Boolean updateAntiHoardPriceById(Long id, BigDecimal initAntiHoardPrice) {
        UpdateWrapper<UserCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("anti_hoard_price", initAntiHoardPrice);
        return update(wrapper);
    }

    /**
     * 查询用户当前持有的藏品Id
     *
     * @return
     */
    public List<Long> getHoldCollectionId(Long userId) {
        QueryWrapper<UserCollection> wrapper = new QueryWrapper<>();
        wrapper.select("distinct collection_id");
        wrapper.eq("user_id", userId);
        wrapper.eq("is_hold", Boolean.TRUE);
        wrapper.eq("is_open", Boolean.TRUE);
        return listObjs(wrapper, x -> (Long) x);
    }

    /**
     * 查询用户购买过  的藏品Id
     *
     * @return
     */
    public List<Long> getHoldCollectionIdFormOrder(Long userId) {
        QueryWrapper<UserCollection> wrapper = new QueryWrapper<>();
        wrapper.select("distinct collection_id");
        wrapper.eq("user_id", userId);
        wrapper.in("type", ReceivedCollectionType.BUY.getCode(), ReceivedCollectionType.CONSIGN.getCode());
        return listObjs(wrapper, x -> (Long) x);
    }

    public void addDisassemblyAmountById(BigDecimal disassemblyAmount, Long id) {
        UpdateWrapper<UserCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.setSql("disassembly_amount = disassembly_amount + " + disassemblyAmount);
        update(wrapper);
    }


    public BigDecimal getUserCountWithPriceAndCollectionIds(BigDecimal minPrice, BigDecimal maxPrice, List<Long> collectionId) {
        return baseMapper.getUserCountWithPriceAndCollectionIds(minPrice, maxPrice, collectionId);
    }

    public BigDecimal getSumPriceWithPriceAndCollectionIds(BigDecimal minPrice, BigDecimal maxPrice, List<Long> collectionId) {
        return baseMapper.getSumPriceWithPriceAndCollectionIds(minPrice, maxPrice, collectionId);
    }

    public int getIsContainsAndIsHoldAndIsOpenAndOnConsignByCollectionId(Long collectionId, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        return count(wrapper);
    }

    public int getIsContainsAndIsHoldAndIsOpenAndOnConsignByCollectionIds(List<Long> collectionIds, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserCollection::getCollectionId, collectionIds);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        return count(wrapper);
    }

    public List<UserCollection> getUserCollectionListByCollectionIdAndUserIdAndSize(Long collectionId, Long userId, int size) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL.getCode());
        wrapper.orderByDesc(UserCollection::getLockedPositionTime);
        wrapper.last("limit " + size);
        return list(wrapper);
    }

    /**
     * 查询新增数量
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public Integer getNewAddCount(Date startTime, Date endTime, int level) {
        return baseMapper.getNewAddCount(startTime, endTime, level);
    }


    public Page<MallUserCollectionVO> pageMallUserCollection(MallUserCollectionDTO mallUserCollectionDTO) {
        return baseMapper.pageMallUserCollection(new Page<>(mallUserCollectionDTO.getPageNo(), mallUserCollectionDTO.getPageSize()), mallUserCollectionDTO.getUserId(), mallUserCollectionDTO.getCollectionIds());
    }

    public Page<UserHoldCollectionVO> getPageByUserIdAndCollectionId(Long userId, Long collectionId, Integer pageNo, Integer pageSize) {
        return baseMapper.getPageByUserIdAndCollectionId(new Page<>(pageNo, pageSize), userId, collectionId);
    }

    public List<UserCollection> getListByUserIdAndCollectionIdAndIds(Long userId, Long collectionId, List<Long> userCollectionIdList) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.in(UserCollection::getId, userCollectionIdList);
        return list(wrapper);
    }

    public List<UserCollection> getPledgeListByUserIdAndCollectionIdAndIds(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        return list(wrapper);
    }

    public List<UserCollection> getListByUserIdAndCollectionIdAndCount(Long userId, Long collectionId, Integer collectionPledgeCount) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getStatus, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.le(UserCollection::getLockedPositionTime, DateTime.now());
        wrapper.last("limit " + collectionPledgeCount);
        return list(wrapper);
    }

    public Integer getHoldByUserIdAndCollectionIdAndCount(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getStatus, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.le(UserCollection::getLockedPositionTime, DateTime.now());
        return count(wrapper);
    }

    public List<UserCollection> getListByUserIdAndCollectionIdAndCount(Long userId, Long collectionId, List<Long> userCollectionIdList) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getStatus, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.le(UserCollection::getLockedPositionTime, DateTime.now());
        wrapper.in(ObjectUtil.isNotNull(userCollectionIdList), UserCollection::getId, userCollectionIdList);
        return list(wrapper);
    }

    public List<UserCollection> pledgeSecretMaxCount(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getStatus, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.lt(UserCollection::getLockedPositionTime, DateTime.now());
        return list(wrapper);
    }

    public List<UserCollection> getUserCollectionByCollectionIdAndLimit(Long userId, Long collectionId, Integer limit) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.orderByDesc(UserCollection::getIsSmeltingOnly, UserCollection::getLockedPositionTime);
        wrapper.last("limit " + limit);
        return list(wrapper);
    }

    public Boolean updateFreezingStatusById(Long id, UserCollectionFreezingStatusEnum freezingStatus) {
        UpdateWrapper<UserCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("freezing_status", freezingStatus.getCode());
        wrapper.eq("status", ONLINE.getCode());
        wrapper.eq("is_smelting_only", Boolean.FALSE);
        wrapper.le("locked_position_time", DateTime.now());
        return update(new UserCollection(), wrapper);
    }

    public Boolean updateFreezingStatusByIds(List<Long> ids, UserCollectionFreezingStatusEnum freezingStatus) {
        UpdateWrapper<UserCollection> wrapper = new UpdateWrapper<>();
        wrapper.in("id", ids);
        wrapper.set("freezing_status", freezingStatus.getCode());
        return update(new UserCollection(), wrapper);
    }

    public Boolean whetherTheUserOwnsACollection(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        return count(wrapper) > 0;
    }

    public Boolean updateUuIdAndNumberingById(Long id, String collectionUuid, Integer numbering) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.set(UserCollection::getCollectionUuid, collectionUuid);
        wrapper.set(UserCollection::getNumbering, numbering);
        return update(wrapper);
    }

    public List<UserCollection> getCollectionList(long startId, int end) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getStatus, ChainStatusEnum.NONE);
        wrapper.gt(UserCollection::getId, startId);
        wrapper.last("limit " + end);
        return list(wrapper);
    }

    public Boolean updateStatusAndChainUrlById(Long id, ChainStatusEnum chainStatusEnum, String empty) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.set(UserCollection::getStatus, chainStatusEnum);
        wrapper.set(UserCollection::getChainUrl, empty);
        return update(wrapper);
    }

    /**
     * 获取用户的某个藏品的持有数量,
     * 质押也算进去
     *
     * @param userId
     * @param collectionId
     * @return
     */
    public Integer applicationRequirement(Long userId, Long collectionId) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return count(wrapper);
    }

    /**
     * 获取用户的某个藏品的持仓成本
     *
     * @param
     * @return
     */
    public BigDecimal getTheTotalPerformanceOfTheUserTeam(Long userId, Integer day, Boolean urbanServiceCenter) {
        return baseMapper.getTheTotalPerformanceOfTheUserTeam(userId, day, urbanServiceCenter);
    }

    public Page<UserCollectionBySalvageEventsIdVO> getUserCollectionBySalvageEventsId(Long userId, List<Long> colectionIds, Integer pageNo, Integer pageSize) {
        return baseMapper.getUserCollectionBySalvageEventsId(new Page<>(pageNo, pageSize), userId, colectionIds);
    }

    public List<UserCollection> getCollectionIdByIds(Long userId, List<Long> userCollectionIds) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getIsHold, true);
        wrapper.eq(UserCollection::getIsOpen, true);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.in(UserCollection::getId, userCollectionIds);
        wrapper.lt(UserCollection::getLockedPositionTime, DateTime.now());
        return list(wrapper);
    }


    public Integer getByUserIdAndCollectionIdCount(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserCollection::getUserId, userId);
        lambdaQueryWrapper.eq(UserCollection::getCollectionId, collectionId);
        lambdaQueryWrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return count(lambdaQueryWrapper);
    }

    public Boolean recoverCollectionById(Long id, Long toUserId) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsRecover, Boolean.FALSE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getStatus, Boolean.TRUE);

        wrapper.set(UserCollection::getToUserId, toUserId);
        wrapper.set(UserCollection::getIsHold, Boolean.FALSE);
        wrapper.set(UserCollection::getIsRecover, Boolean.TRUE);
        wrapper.set(UserCollection::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public Boolean temporarilyRecoverCollectionById(Long id, Long toUserId) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.set(UserCollection::getToUserId, toUserId);
        wrapper.set(UserCollection::getIsRecover, Boolean.TRUE);
        wrapper.set(UserCollection::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public Boolean ecologyDestroyCollections(Long id) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getId, id);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsRecover, Boolean.FALSE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getStatus, Boolean.TRUE);

        wrapper.set(UserCollection::getIsHold, Boolean.FALSE);
        wrapper.set(UserCollection::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public List<UserCollection> getListByIds(List<Long> ids) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserCollection::getId, ids);
        return list(wrapper);
    }

    public List<UserCollection> getCollectionListByIds(List<Long> ids) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserCollection::getId, ids);
        return list(wrapper);
    }


    public List<UserCollection> getNotInUserCollectionChaingList(Long collectionId) {
        return baseMapper.getNotInUserCollectionChaingList(collectionId);
    }

    /**
     * 后端市值账号查自己拥有的藏品使用的功能sql.不允许作为他用
     *
     * @param userId
     * @return
     */
    public List<MyCollectionNowListVO> getUserInviteListNow(Long userId) {
        return baseMapper.getUserInviteListNow(userId);
    }

    /**
     * 后端市值账号查自己拥有的藏品使用的功能sql.不允许作为他用 自由市场
     *
     * @param userId
     * @return
     */
    public List<MyCollectionNowListVO> getMaintainInviteListNow(Long userId) {
        return baseMapper.getMaintainInviteListNow(userId);
    }

    public MyCollectionNowListVO getMaintainCollection(Long userId, Long collectionId) {
        return baseMapper.getMaintainCollection(userId, collectionId);
    }

    public List<UserCollection> selectByCollectionIds(List<Long> list) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserCollection::getCollectionId, list);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.in(UserCollection::getStatus, ONLINE, ChainStatusEnum.NONE);
        return list(wrapper);
    }

    public Integer getUserHoldCollectionByIds(Long userId, List<Long> collectionIds) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.in(UserCollection::getCollectionId, collectionIds);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return count(wrapper);
    }

    public List<Long> getUserListByCollectionIds(List<Long> collectionIds) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(UserCollection::getUserId);
        wrapper.in(UserCollection::getCollectionId, collectionIds);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.last("group by user_id");
        return listObjs(wrapper, (obj) -> (Long) obj);
    }

    public Page<MallCollectionUserVO> pageUserListByCollectionIds(Page<UserCollection> page, List<Long> collectionIds) {
        return baseMapper.pageUserListByCollectionIds(page, collectionIds);
    }

    public List<UserCollection> getListByCollectionIds(List<Long> collectionIds) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserCollection::getCollectionId, collectionIds);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return baseMapper.selectList(wrapper);

    }

    public List<Long> getUserIdByHoldCollectionId(Long collectionId) {
        QueryWrapper<UserCollection> wrapper = Wrappers.query();
        wrapper.select("distinct user_id");
        wrapper.eq("collection_id", collectionId);
        wrapper.eq("is_hold", true);
        wrapper.eq("is_open", true);
        wrapper.eq("is_delete", false);
        return listObjs(wrapper, (x) -> (Long) x);
    }

    public List<Long> getUserIdByCollectionId(Long collectionId) {
        QueryWrapper<UserCollection> wrapper = Wrappers.query();
        wrapper.select("user_id");
        wrapper.eq("collection_id", collectionId);
        wrapper.eq("is_hold", true);
        wrapper.eq("is_open", true);
        wrapper.eq("is_delete", false);
        return listObjs(wrapper, (x) -> (Long) x);
    }

    public List<Long> getJuniorHoldCollectionCountByCollectionIds(Long userId, List<Long> collectionIds) {
        return baseMapper.getJuniorHoldCollectionCountByCollectionIds(userId, collectionIds);
    }


    public List<UserHoldCollectionDTO> getHoldCountByCollectionIdsAndUserId(Long userId, List<Long> collectionIds) {
        return baseMapper.getHoldCountByCollectionIdsAndUserId(userId, collectionIds);
    }

    public Integer getFreeUserCount() {
        return baseMapper.getFreeUserCount();
    }


    public List<TeamHoldCollectionVO> getTeamHoldCollectioncCountByCollectionId(Long collectionId, String userTel) {
        return baseMapper.getTeamHoldCollectioncCountByCollectionId(collectionId, userTel);
    }

    public Integer daoTeamHoldCollectionCount(Long daoUserId, Long collectionId, String userTel) {
        return baseMapper.daoTeamHoldCollectionCount(daoUserId, collectionId, userTel);
    }

    public List<TeamHoldCollectionVO> daoTeamHoldCollection(Long daoUserId, Long collectionId, String userTel) {
        return baseMapper.daoTeamHoldCollection(daoUserId, collectionId, userTel);
    }

    public List<UserHoldVO> getUserHoldByCollectionId(Long collectionId) {
        return baseMapper.getUserHoldByCollectionId(collectionId);
    }

    public IPage<MyBoxTransferVO> myBoxTransfer(Long userId, Integer pageNo, Integer pageSize) {
        return baseMapper.myBoxTransfer(new Page<>(pageNo, pageSize), userId);
    }

    public IPage<MyBoxReceiveVO> myBoxReceive(Long userId, Integer pageNo, Integer pageSize) {
        return baseMapper.myBoxReceive(new Page<>(pageNo, pageSize), userId);
    }

    public Page<BoxGroupVO> boxGroupUser(Long userId, Integer pageNo, Integer pageSize) {
        return baseMapper.boxGroupUser(new Page<>(pageNo, pageSize), userId);
    }

    public BoxGroupVO boxGroupUser(Long userId, Long boxId, Long taskPoolId) {
        return baseMapper.countBoxGroupUser(userId, boxId, taskPoolId);
    }

    /**
     * 获取用户系列盲盒列表
     *
     * @param boxGroupListDTO
     * @param user
     * @return
     */
    public IPage<BoxGroupListVO> boxGroupList(BoxGroupListDTO boxGroupListDTO, UserInfo user) {
        return baseMapper.boxGroupList(boxGroupListDTO.toMp(), boxGroupListDTO, user);
    }

    /**
     * 用户持仓（智探）
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    public List<ZhiTanUserHoldVO> userHold(Long userId, Integer page, Integer pageSize) {
        return baseMapper.userHold(new Page<>(page, pageSize), userId).getRecords();
    }


    public List<SaasTaskConditionVO> saasTaskCondition(Long taskId, Long userId) {
        return baseMapper.saasTaskCondition(taskId, userId);
    }

    public List<UserCollection> getByCollectionIdAndUserId(List<Long> collectionId, Long userId) {
        if (CollUtil.isEmpty(collectionId)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.in(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return list(wrapper);
    }

    public List<UserCollection> getByCollectionIdAndUserIdAndSize(List<Long> collectionId, Long userId, Integer size) {
        if (CollUtil.isEmpty(collectionId)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.in(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.orderByAsc(UserCollection::getId);
        wrapper.last("Limit " + size);
        return list(wrapper);
    }

    /**
     * 一键导入用户持仓（仅盯链）
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    public Page<OneClickImportVO> oneClickImport(Long userId, Integer page, Integer pageSize) {
        return baseMapper.oneClickImport(new Page<>(page, pageSize), userId);
    }

    public Integer getByUserIdAndCollectionIdCountGetTime(Long userId, Long collectionId, Date beginTime, Date endTime) {
        LambdaQueryWrapper<UserCollection> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserCollection::getUserId, userId);
        lambdaQueryWrapper.eq(UserCollection::getCollectionId, collectionId);
        lambdaQueryWrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        lambdaQueryWrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        lambdaQueryWrapper.between(UserCollection::getCreateTime, beginTime, endTime);
        return count(lambdaQueryWrapper);
    }

    public Integer getPledgeSecretByUserIdAndCollectionId(Long id, Long collectionId) {
        return baseMapper.getPledgeSecretByUserIdAndCollectionId(id, collectionId);
    }

    public Integer getCollectionCountByCollectionId(Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return count(wrapper);
    }

    public Integer getPledgeCountByCollectionId(Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.in(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.VERY_ACTIVE, UserCollectionFreezingStatusEnum.PRESSING, UserCollectionFreezingStatusEnum.LOCK);
        return count(wrapper);
    }


    /**
     * 用户已兑换藏品初始化专用
     *
     * @param relationId
     * @param id
     * @param limit
     * @return
     */
    public List<UserCollection> queryByProductIdForInit(Long relationId, Long id, Integer limit, Boolean isCollection) {
        LambdaQueryWrapper<UserCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(SuperEntity::getIsDelete, 1);
        queryWrapper.eq(UserCollection::getIsHold, 1);
        queryWrapper.ne(UserCollection::getIsConsume, 1);
        if (isCollection) {
            queryWrapper.eq(UserCollection::getCollectionId, relationId);
            queryWrapper.isNull(UserCollection::getBoxId);
            queryWrapper.eq(UserCollection::getType, 14);
            queryWrapper.ne(UserCollection::getIsOpen, 0);
        } else {
            queryWrapper.eq(UserCollection::getBoxId, relationId);
            queryWrapper.eq(UserCollection::getType, 12);
            queryWrapper.ne(UserCollection::getIsOpen, 1);
        }
//        queryWrapper.ne(UserCollection::getIsOpen, isCollection ? 0 : 1);
        queryWrapper.gt(SuperEntity::getId, id);
        queryWrapper.last(" limit " + limit);
        return list(queryWrapper);
    }


    /**
     * 批处理
     *
     * @param userCollections
     * @param boxTakePoolId
     */
    public void initUserCollection(List<UserCollection> userCollections, Long boxTakePoolId) {

        if (userCollections.size() > 0) {
            int resultCount = this.updateSession(userCollections, boxTakePoolId);
            log.info("更新用户藏品关联活动id完成，数量：{}", resultCount);
        }

    }

    public int updateSession(List<UserCollection> list, Long boxTakePoolId) {
        SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        try {
            // 获取批量操作的新的map
            UserCollectionMapper mapper = batchSqlSession.getMapper(UserCollectionMapper.class);

            for (UserCollection userCollection : list) {
                mapper.updateUserCollectionById(boxTakePoolId, userCollection.getId());
            }
            // session 提交
            batchSqlSession.commit();
        } catch (Exception e) {
            batchSqlSession.rollback();
        } finally {
            // session 关闭
            batchSqlSession.close();
        }
        return list.size();
    }


    public Page<MyCollectionNowVO> myCollectionGroupByCollection(String isPrivilege, Long userId, int pageNo, int pageSize) {
        return baseMapper.myCollectionNow(new Page(pageNo, pageSize), userId, isPrivilege);
    }

    public List<SimpleUserCollectionVO> getBatchGiveMetaWalletDetails(Long userId, Long collectionId) {
        return baseMapper.getBatchGiveMetaWalletDetails(userId, collectionId);
    }

    public List<UserCollection> getGiveListByCollectionIdAndUserCollectionIds(Long userId, Long collectionId, List<Long> ids, List<Long> secondCollectionIds) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserCollection::getId, ids);
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.lt(UserCollection::getLockedPositionTime, DateTime.now());
        wrapper.notIn(UserCollection::getCollectionId, secondCollectionIds);
        return list(wrapper);
    }

    public boolean giveMetaWallet(Long userCollectionId) {
        LambdaUpdateWrapper<UserCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserCollection::getId, userCollectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.lt(UserCollection::getLockedPositionTime, DateTime.now());

        wrapper.set(UserCollection::getGiveStatus, GiveStatusEnum.GIVE_META_WALLER);
        wrapper.set(UserCollection::getIsHold, Boolean.FALSE);
        wrapper.set(UserCollection::getToUserId, 1l);
        return update(new UserCollection(), wrapper);
    }

    public List<MyCollectionCountVO> getByUserAndIdList(Long userId, List<Long> collectionIdList) {
        return baseMapper.getByUserAndIdList(userId, collectionIdList);
    }

    /**
     * 根据用户Id获取用户持有的藏品（已上链）
     *
     * @param userId
     * @return
     */
    public List<UserCollection> getCollectionIdByUserId(Long userId, List<Long> holdCollectionIds) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getStatus, ONLINE.getCode());
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.in(UserCollection::getCollectionId, holdCollectionIds);
        return list(wrapper);
    }

    public Boolean getUpgradeCollectionCountByLevel(Long userId, int level) {
        return baseMapper.getUpgradeCollectionCountByLevel(userId, level) >= 1;
    }

    public List<UserCollection> getGoalCollection() {
        return baseMapper.getGoalCollection();
    }


    /**
     * 获取某个用户下面某个时间段内符合条件增长的用户数量
     *
     * @param userId
     * @return
     */
    public Integer getNationalTreasuryIncreaseCount(Long userId, Date startTime, Date endTime) {
        return baseMapper.getNationalTreasuryIncreaseCount(userId, startTime, endTime);
    }

    public List<UserCollection> listByIdListAndUserId(Long userId, List<Long> idList) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.in(UserCollection::getId, idList);
        return list(wrapper);
    }

    public Page<GiveCollectionListByNewVO> getPageGiveCollectionListByQuery(Long userId, Long collectionId, Integer antiHoardPriceSort, Integer pageNo, Integer pageSize) {
        return baseMapper.getPageGiveCollectionListByQuery(new Page(pageNo, pageSize), userId, collectionId, antiHoardPriceSort);
    }

    /**
     * 获取某些藏品用户持有的数量
     *
     * @param collectionIds
     * @return
     */
    public List<UserCollection> selectByCollectionIdsIsHold(List<Long> collectionIds) {
        LambdaQueryWrapper<UserCollection> userCollectionLambdaQueryWrapper = Wrappers.lambdaQuery();
        userCollectionLambdaQueryWrapper.in(UserCollection::getCollectionId, collectionIds);
        userCollectionLambdaQueryWrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        List<UserCollection> list = list(userCollectionLambdaQueryWrapper);
        return list;
    }

    public List<UserCollection> getListByIdListAndDelete(List<Long> userCollectionIdList) {
        return baseMapper.getListByIdListAndDelete(userCollectionIdList);
    }

    public Page<MyCopyrightCollectionNowVO> myCopyrightCollectionNow(Long userId, Integer pageNo, Integer pageSize) {
        return baseMapper.myCopyrightCollectionNow(new Page(pageNo, pageSize), userId);
    }

    public UserCollection getByUserIdAndUserCollectionId(Long userId, Long userCollectionId) {
        LambdaQueryWrapper<UserCollection> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserCollection::getId, userCollectionId);
        lambdaQueryWrapper.eq(UserCollection::getUserId, userId);
        lambdaQueryWrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return getOne(lambdaQueryWrapper);
    }

    public Integer consensusQuantity(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        return count(wrapper);
    }

    public UserCollection getUserByCollectionIdOne(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }


    public Integer getCountByCollectionIdAndLtNowDate(Long consumeCollectionId, Long userId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, consumeCollectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getStatus, ONLINE);
        wrapper.eq(UserCollection::getIsSmeltingOnly, Boolean.FALSE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.lt(UserCollection::getLockedPositionTime, DateTime.now());
        return count(wrapper);
    }

    public IPage<UserCollection> userCollectionPage(Page page, Long userId, Long collectionId) {
        LambdaQueryWrapper<UserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollection::getUserId, userId);
        wrapper.eq(UserCollection::getCollectionId, collectionId);
        wrapper.eq(UserCollection::getIsHold, Boolean.TRUE);
        wrapper.eq(UserCollection::getIsOpen, Boolean.TRUE);
        wrapper.eq(UserCollection::getOnConsign, CollectionOnConsignEnum.NORMAL);
        wrapper.eq(UserCollection::getFreezingStatus, UserCollectionFreezingStatusEnum.NORMAL);
        wrapper.orderByAsc(UserCollection::getNumbering);
        return page(page, wrapper);
    }


    public List<UserHoldCollectionDTO> getHoldCountByCollectionIdsAndUserIdList(List<Long> userIdList, List<Long> collectionIds) {
        return baseMapper.getHoldCountByCollectionIdsAndUserIdList(userIdList,collectionIds);
    }
}